<?php
require_once('../../config.php');
Doctrine::loadModels('../../models');

// can combine agent and people into query to return one result set.
// wrote SQL select as unsure of doctrine method when multiple references to same table
if ($_REQUEST["agent_id"]) 
{
	$agent_id = strip_tags($_POST["agent_id"]);
	file_put_contents  ( "param_id.txt", $agent_id);	
	
	$query = $db_query = sprintf("SELECT a.agent_id, a.name_short, a.name_full, 
			a.agent_type_id, a.date_start, a.date_end,
        	p.title_id, p.gender_id, p.primary_role_id, p.email, 
        	p.telephone, p.mobile, p.im_name, p.im_service_id, 
        	p.fax, p.website, p.is_signatory, 
        	g.item_name gender, 
        	r.item_name role, 
        	i.item_name im_service, 
        	t.item_name title
	FROM people p
  		INNER JOIN agent a ON p.agent_person_id = a.agent_id
  		INNER JOIN lk_glossary g ON p.gender_id = g.item_id
  		INNER JOIN lk_glossary r ON p.primary_role_id = r.item_id
  		INNER JOIN lk_glossary i ON p.im_service_id = i.item_id 
  		INNER JOIN lk_glossary t ON p.title_id = t.item_id
	WHERE a.agent_id ='".$agent_id."'");
	
	$items = mysql_query($query);

	 	   	if (!$items) {
        		die('invalid query');    // . mysql_error());
    		}    
	
}


$dom = new DomDocument('1.0',"UTF-8");
$dom->formatOutput = true;
$root = $dom->createElement( "results" );
$dom->appendChild( $root );
	while(($item = mysql_fetch_assoc($items)))
	{
		$dn = $dom->createElement( "record" );
		$dn->setAttribute( 'agent_id', ($item['agent_id']));
		//$dn->setAttribute( 'name_short', utf8_encode($item['name_short']));
		//$dn->setAttribute( 'name_full', utf8_encode($item['name_full']));
		$dn->setAttribute( 'name_short', $item['name_short']);
		$dn->setAttribute( 'name_full', $item['name_full']);
		$dn->setAttribute( 'date_start', $item['date_start'] );
		$dn->setAttribute( 'date_end', $item['date_end'] );
		$dn->setAttribute( 'title_id', $item['title_id'] );
		$dn->setAttribute( 'gender_id', $item['gender_id'] );
		$dn->setAttribute( 'primary_role_id', $item['primary_role_id'] );
		$dn->setAttribute( 'email', ($item['email']));
		$dn->setAttribute( 'telephone', ($item['telephone']));
		$dn->setAttribute( 'mobile', ($item['mobile']));
		$dn->setAttribute( 'im_name', $item['im_name'] );
		$dn->setAttribute( 'im_service_id', $item['im_service_id'] );
		$dn->setAttribute( 'fax', $item['fax'] );
		$dn->setAttribute( 'website', ($item['website'] ));
		$dn->setAttribute( 'is_signatory', $item['is_signatory'] );
		$dn->setAttribute( 'gender', $item['gender'] );
		$dn->setAttribute( 'role', $item['role'] );
		$dn->setAttribute( 'im_service', $item['im_service'] );
		$dn->setAttribute( 'title', $item['title'] );
		$root->appendChild( $dn );
	}		
header( "Content-type: text/xml; charset=UTF-8");
echo $dom->saveXML();


?>